package com.company.genealogy.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.web.entity.Role;
import com.company.genealogy.web.entity.RoleMenu;
import com.company.genealogy.web.service.RoleService;
import com.company.genealogy.web.mapper.RoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.company.genealogy.common.constant.ApiCodeEnum;

@RestController
@RequestMapping("/api/v1/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;
    
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    // 角色列表
    @GetMapping
    public ApiResponse<IPage<Role>> list(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size,
                                       @RequestParam(required = false) String name,
                                       @RequestParam(required = false) Integer status) {
        Page<Role> p = new Page<>(page, size);
        
        // 构建查询条件
        com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Role> queryWrapper = new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>();
        
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name.trim());
        }
        
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        
        IPage<Role> result = roleService.page(p, queryWrapper);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, result);
    }

    // 获取所有启用的角色
    @GetMapping("/enabled")
    public ApiResponse<List<Role>> getEnabledRoles() {
        List<Role> roles = roleService.getEnabledRoles();
        return ApiResponse.success(ApiCodeEnum.SUCCESS, roles);
    }

    // 新增角色
    @PostMapping
    public ApiResponse<Role> create(@RequestBody Role role) {
        roleService.save(role);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, role);
    }

    // 角色详情
    @GetMapping("/{id}")
    public ApiResponse<Role> detail(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, role);
    }

    // 编辑角色
    @PutMapping("/{id}")
    public ApiResponse<Void> update(@PathVariable Long id, @RequestBody Role role) {
        role.setId(id);
        roleService.updateById(role);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }

    // 删除角色
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        roleService.removeById(id);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }
    
    // 设置角色菜单
    @PostMapping("/{id}/menus")
    public ApiResponse<Void> setRoleMenus(@PathVariable Long id, @RequestBody List<Long> menuIds) {
        // 先删除角色现有菜单
        roleMenuMapper.delete(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<RoleMenu>()
                .eq("role_id", id));
        
        // 添加新菜单
        for (Long menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(id);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }
        
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }
} 